import { login, getUserInfo } from '@/api'
import { getToken, setToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
      setToken(token)
    },
    SET_USERINFO(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    // 获取token
    async fetchLogin(store, loginFrom) {
      const { data } = await login(loginFrom)
      store.commit('SET_TOKEN', data)
      return data
    },
    // 获取用户信息
    async fetchUserInfo(store) {
      const { data } = await getUserInfo()
      store.commit('SET_USERINFO', data)
    }
  }
}
